<?php

	function getUserLang($BDD_connexion,$id)
	{
		
		$request = "SELECT ref_langue FROM personne WHERE id = :id";
		$prepare = $BDD_connexion->prepare($request);
		$prepare->bindValue(':id',$id,PDO::PARAM_INT);
		$prepare->execute();
		$langue = $prepare->fetch(PDO::FETCH_OBJ);
		
		return $langue->ref_langue;
		
	}

	function is_pic($path)
	{
		$finfo = finfo_open(FILEINFO_MIME_TYPE);
		$type = finfo_file($finfo, $path); 
		$type = explode("/",$type);
		return $type[0] == "image";
	}

	function makeDefaultImage($filename,$folder)
	{
		
		if (file_exists($folder."default.png"))
		{
			rename($folder."default.png",$folder."oui_".date("YmdHis").".png");
		}
		
		return rename($folder.$filename,$folder."default.png");
		
	}
	
	function convertToPng($file,$folder,$delete = FALSE)
	{
		
		$filepath = $folder.$file;
		
		$size = getimagesize($folder.$file);
		
		$fileextension = explode(".",$file);
		switch ($fileextension[count($fileextension)-1]) {
			case "png":
			$source = imagecreatefrompng($folder.$file);
			break;
			
			case "jpg":
			$source = imagecreatefromjpeg($folder.$file);
			break;
			
			case "bmp":
			$source = imagecreatefrombmp($folder.$file);
			break;
		}
		
		$destination = imagecreatetruecolor($size[0],$size[1]);
		imagecopy($destination, $source, 0, 0, 0, 0, $size[0], $size[1]);
		
		imagepng($destination, $folder.substr($file,0,-3)."png");
		chmod($folder.substr($file,0,-3)."png",0777);
		
		if ($delete)
		{
			unlink($folder.$file);
		}
			
	}

	function alreadyExistsLogin($BDD_connexion,$login) {

	$request = $BDD_connexion->prepare("
		SELECT COUNT(*) AS number FROM personne WHERE personne.login = :login");
	$request->bindValue(':login', $login, PDO::PARAM_STR);
	$request->execute();
	$result = $request->fetch(PDO::FETCH_OBJ);
	
	return ($result->number > 0);

	}
	
	function alreadyExistsMail($BDD_connexion,$mail) {

	$request = $BDD_connexion->prepare("
		SELECT COUNT(*) AS number FROM personne WHERE personne.mail = :mail");
	$request->bindValue(':mail', $mail, PDO::PARAM_STR);
	$request->execute();
	$result = $request->fetch(PDO::FETCH_OBJ);

	return ($result->number > 0);

	}
	
	function getUserIdByMail($BDD_connexion,$mail) {
		
		$request = $BDD_connexion->prepare("
			SELECT id FROM personne WHERE personne.mail = :mail");
		$request->bindValue(':mail', $mail, PDO::PARAM_STR);
		$request->execute();
		$result = $request->fetch(PDO::FETCH_OBJ);

		if (isset($result->id))
		{
			return $result->id;
		}
		else
		{
			return 0;
		}
		
	}

	function lastExists()
	{
		if (isset($_SERVER["HTTP_REFERER"]))
		{
			$last_path = explode("/",$_SERVER["HTTP_REFERER"]);
			$last_path = $last_path[count($last_path)-1];
		}
		else
		{
			$last_path = "";
		}
		return $last_path;
	}
	
	function isValidSessionUser($BDD_connexion)
	{
		return (isset($_SESSION['user']) && isset($_SESSION['passhash']) && isValidUser($BDD_connexion,$_SESSION['user'],$_SESSION['passhash']));
	}
	
	function areValidUserRights($BDD_connexion)
	{
		return (isset($_SESSION['userRights']) && $_SESSION['userRights'] == getUserRights($BDD_connexion,$_SESSION['userid']));
	}

	function isValidUser($BDD_connexion,$username,$passwordhash)
	{
		$user = htmlspecialchars($username);
		
		$request = "SELECT valide FROM personne WHERE login = :login AND mdp = :mdp";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindParam(':login',$user,PDO::PARAM_STR);
		$prepare->bindParam(':mdp',$passwordhash,PDO::PARAM_STR);
		$prepare->execute();
		$user = $prepare->fetch(PDO::FETCH_OBJ);
		
		return (isset($user->valide) && ($user->valide == "1"));
		
	}
	
	function getUserId($BDD_connexion,$username,$passwordhash)
	{
		$user = htmlspecialchars($username);
		
		$request = "SELECT id FROM personne WHERE login = :login AND mdp = :mdp";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindParam(':login',$user,PDO::PARAM_STR);
		$prepare->bindParam(':mdp',$passwordhash,PDO::PARAM_STR);
		$prepare->execute();
		$userId = $prepare->fetch(PDO::FETCH_OBJ);
		
		return $userId->id;
	}
	
	function getUserDatas($BDD_connexion,$userid)
	{
		
		$request = "SELECT * FROM personne WHERE id = :id";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindValue(':id',$userid,PDO::PARAM_INT);
		$prepare->execute();
		$user = $prepare->fetch(PDO::FETCH_OBJ);
		
		return $user;
	}
	
	function getUserAddress($BDD_connexion,$userid)
	{

		$request = "SELECT * FROM adresse WHERE id_adresse = (SELECT ref_adresse FROM personne WHERE id = :id)";
		$prepare = $BDD_connexion->prepare($request);
		$prepare->bindValue(':id',$userid,PDO::PARAM_INT);
		$prepare->execute();
		$adresse = $prepare->fetch(PDO::FETCH_OBJ);
		
		return $adresse;
		
	}
	
	function getUserRights($BDD_connexion,$userid)
	{

		$request = "SELECT label FROM categorie WHERE id_categorie = (SELECT ref_categorie FROM personne WHERE id = :id)";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindValue(':id',$userid,PDO::PARAM_INT);
		$prepare->execute();
		$userRights = $prepare->fetch(PDO::FETCH_OBJ);
		
		return $userRights;
	}

	function isOwner($BDD_connexion,$userid)
	{
		return getUserRights($BDD_connexion,$userid)->label == "proprietaire";
	}
	
	function getMailOfOwner($BDD_connexion,$userid)
	{
		
		$request = "SELECT mail FROM personne WHERE id = :id";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindValue(':id',$userid,PDO::PARAM_INT);
		$prepare->execute();
		$userMail = $prepare->fetch(PDO::FETCH_OBJ);
		
		return $userMail;
	}

	function isint($value) {
		return (preg_match( '/^\d*$/'  , $value) == 1);
	}
	
	function getLocationDescription($BDD_connexion,$location_id)
	{
		$request = "SELECT content FROM descriptions WHERE id_bien = :id AND id_langue = :langue";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->bindValue(':id',$location_id,PDO::PARAM_INT);
		$prepare->bindValue(':langue',$_SESSION['langid'],PDO::PARAM_INT);
		$prepare->execute();
		$location=$prepare->fetch(PDO::FETCH_OBJ);
		
		if (isset($location->content))
		{
			return $location->content;
		}
		else
		{
			return "";
		}
		
	}

	function getLocationNotation($BDD_connexion, $location_id)
	{
		
		$sum = 0;
		$nb_votes = 0;
		$notation = -1;
			
		$request_count = "SELECT COUNT(*) as count FROM avis_utilisateur WHERE id_bien = :id AND authorized";
		$prepare=$BDD_connexion->prepare($request_count);
		$prepare->bindValue(':id',$location_id,PDO::PARAM_INT);
		$prepare->execute();
		$nb_votes = $prepare->fetch(PDO::FETCH_OBJ);
		
		$nb_votes = $nb_votes->count;
		
		if ($nb_votes > 0)
		{
		
			$request_sum = "SELECT SUM(notation) as sum FROM avis_utilisateur WHERE id_bien = :id AND authorized";
			$prepare=$BDD_connexion->prepare($request_sum);
			$prepare->bindValue(':id',$location_id,PDO::PARAM_INT);
			$prepare->execute();
			$sum = $prepare->fetch(PDO::FETCH_OBJ);
			
			$sum = $sum->sum;
			
			$notation = intval($sum/$nb_votes);
			
		}
		
		return $notation;
		
	}
	
	function default_file_extension($path)
	{
		$exists = "";
		
		$extensions = array("png","jpg","gif");
		foreach ($extensions as $extension)
		{
			if (file_exists($path.".".$extension))
			{
				return $path.".".$extension;
			}
		}
	}
	
function getLocationFieldset($location,$language)
{

	$return = 		'	<fieldset style="float:left; width:47%;">
					<legend>'.strtoupper($location->nom).' ('.$location->categorie.')</legend>
					<div style="float:left;text-align:center;">';
	
	$location_image = default_file_extension("Locations/".$location->id_bien."/default");
	if (file_exists($location_image))
	{
		$return .= '<img src="'.$location_image.'" class="img256rounded">';
	}
	else
	{
		$return .= '<img src="CSS/no_default_image.png" class="img256rounded">';
	}
					
	$return .= 		'	<form method="GET" action="location.php" style="padding-top:5px;">
					<input type="hidden" name="id" value="'.$location->id_bien.'" />
					<input type="submit" value=" '.$language[$_SESSION['lang']]["Accesslocation_button"].' "/>
					</form>
					</div>
					<div style="float:left; padding:10px;">'.$location->description.'</div>
					<div style="clear:left;float:right;">';
					
	if ($location->notation > 0)
	{			
					
		$return .=		'<img src="CSS/Note/'.intval($location->notation).'.png" alt="'.intval($location->notation).' / 10" title="'.intval($location->notation).' / 10"/></div>
						</fieldset>';
	
	}
	else
	{
		
		$return .= 		$language[$_SESSION['lang']]["NotRatedYet"].'
						</fieldset>';
	
	}
		
	return $return;
				
}

?>
